package com.mtt.erp.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.kdniao.KdniaoUtils;
import com.mtt.erp.logistics.dto.FaceOrderPrintDto;
import com.mtt.erp.logistics.dto.OrderFaceTempSaveDto;
import com.mtt.erp.logistics.service.IOrderFaceTempleteService;
import com.mtt.erp.order.dto.MbaFileDto;
import com.mtt.erp.order.dto.MbaFileShipmentDto;
import com.mtt.erp.order.service.IMbaFileService;
import com.mtt.erp.order.vo.MbaFileVo;
import com.mtt.erp.order.vo.OrderDetailVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;

/**
 * @author guomengzhou
 * @Description:订单模块Controller
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/mbaFile")
@Api(value = "订单接口",tags = "M009-订单-订单模块相关接口")
public class MbaFileController{

    @Autowired
    private IMbaFileService iMbaFileService;

    @Autowired
    IOrderFaceTempleteService iOrdeFaceTempleteService;

    /**
     * 根据条件获取订单列表
     *
     * @param request
     * @param mbaFileDto
     * @return
     */
    @Log("查询订单")
    @GetMapping
    @RequiresPermissions("mbaFile:view")
    @ApiOperation(value = "查询订单",notes = "分页查询:查询订单信息")
    public R<IPage<MbaFileVo>> orderList(QueryRequest request, MbaFileDto mbaFileDto) {
        IPage<MbaFileVo> mbaFileVoIPage = this.iMbaFileService.orderList(request, mbaFileDto);
        if(mbaFileVoIPage != null){
            return R.data(mbaFileVoIPage);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    /**
     * 获取订单详情和订单包含商品信息
     *
     * @param mba00
     * @return
     */
    @Log("查询订单详情")
    @GetMapping("/{mba00}")
    @ApiOperation(value = "查询订单详情",notes = "根据id查询订单详情,传入mba00(id)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mba00",value = "订单id",required = true,paramType = "path")
    })
    public R<OrderDetailVo> orderDetail(@NotBlank(message = "{required}") @PathVariable String mba00) {
        OrderDetailVo orderDetailVo = this.iMbaFileService.orderDetail(mba00);
        if(orderDetailVo != null){
            return R.data(orderDetailVo);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    /**
     * 查询订单的信息
     * @param mba00
     * @return
     */
    @Log("查询订单信息")
    @GetMapping("/orderInfo/{mba00}")
    @ApiOperation(value = "查询订单信息",notes = "根据id查询订单信息(打印电子面单时使用),传入mba00(id)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mba00",value = "订单id",required = true,paramType = "path")
    })
    public R<MbaFileVo> orderInfo(@NotBlank(message="{required}") @PathVariable String mba00) {
        MbaFileVo orderInfo = this.iMbaFileService.getOrderInfo(mba00);
        if(orderInfo!=null){
            return R.data(orderInfo);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    /**
     *
     * 订单发货 修改mbaFile
     * @param mbaFileShipmentDto
     */
    @Log("订单发货")
    @PutMapping("mbaFileShipment")
    @ApiOperation(value = "订单发货",notes = "订单发货,传入发货信息")
    public R mbaFileShipment(MbaFileShipmentDto mbaFileShipmentDto){
        try {
            Boolean shipment = this.iMbaFileService.mbaFileShipment(mbaFileShipmentDto);
            if(shipment){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e){
            return R.fail(ResultCode.FAILURE);
        }
    }
}
